package dmt16.TelemetrySuit.UI.PlotTools
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.events.DragEvent;
	
	public class GraphNavigator extends Sprite
	{
		public function GraphNavigator()
		{
			addChild(bmp);
			addChild(_window);
			addChild(_handleLeft);
			addChild(_handleRight);
			
			_handleLeft.graphics.beginFill(0xFFFFFF,0.4);
			_handleLeft.graphics.drawCircle(0,0,8);
			_handleLeft.graphics.beginFill(0xFFFFFF,1);
			_handleLeft.graphics.drawCircle(0,0,5);
			
			_handleRight.graphics.beginFill(0xFFFFFF,0.4);
			_handleRight.graphics.drawCircle(0,0,8);
			_handleRight.graphics.beginFill(0xFFFFFF,1);
			_handleRight.graphics.drawCircle(0,0,5);
			
			_handleLeft.addEventListener(MouseEvent.MOUSE_OVER,_onHandleMouseOver);
			_handleRight.addEventListener(MouseEvent.MOUSE_OVER,_onHandleMouseOver);
			
			_handleLeft.addEventListener(MouseEvent.MOUSE_OUT,_onHandleMouseOut);
			_handleRight.addEventListener(MouseEvent.MOUSE_OUT,_onHandleMouseOut);
			
			_handleLeft.addEventListener(MouseEvent.MOUSE_DOWN,_onHandleMouseDown);
			_handleRight.addEventListener(MouseEvent.MOUSE_DOWN,_onHandleMouseDown);
			
			_drawHandles();
		}
		
		public var bmp:Bitmap=new Bitmap();
		private var _window:Shape=new Shape();
		private var _handleLeft:Sprite=new Sprite();
		private var _handleRight:Sprite=new Sprite();
				
		private var _startPos:Number=0;
		public function set startPosition(value:Number):void
		{
			_startPos = value;
		}
		public function get startPosition():Number
		{
			return _startPos;
		}
		
		private var _endPos:Number=1;
		public function set endPosition(value:Number):void
		{
			_endPos = value;
		}
		public function get endPosition():Number
		{
			return _endPos;
		}

		private var _width:Number=200;

		public override function get width():Number{
			return _width;
		}
		public override function set width(value:Number):void{
			_width=value;
			redraw();
		}
		
		private var _height:Number=100;
		public override function get height():Number{
			return _height;
		}
		public override function set height(value:Number):void{
			_height=value;
			redraw();
		}
		
		private function _drawHandles():void{
			_window.graphics.clear();
			_window.graphics.beginFill(0xFFFFFF,0.4);
			_window.graphics.drawRoundRect(_width*_startPos,0,_width*(_endPos-_startPos),_height,8,8);
			_handleLeft.x=_width*_startPos;
			_handleLeft.y=_height/2;
			_handleRight.x=_width*_endPos;
			_handleRight.y=_height/2;
		}
		
		private function _onHandleMouseOver(event:MouseEvent):void{
			var h:Sprite=event.target as Sprite;
			h.graphics.clear();
			h.graphics.beginFill(0xFFFFFF,1);
			h.graphics.drawCircle(0,0,8);
			h.graphics.endFill();
		}
		
		private function _onHandleMouseOut(event:MouseEvent):void{
			var h:Sprite=event.target as Sprite;
			h.graphics.clear();
			h.graphics.beginFill(0xFFFFFF,0.4);
			h.graphics.drawCircle(0,0,8);
			h.graphics.beginFill(0xFFFFFF,1);
			h.graphics.drawCircle(0,0,5);
			h.graphics.endFill();
		}
		
		private var _activeHandle:Sprite;
				
		private function _onHandleMouseDown(event:MouseEvent):void{
			_activeHandle=event.target as Sprite;
			
			var bounds:Rectangle;
			if(_activeHandle==_handleLeft){
				bounds=new Rectangle(0,_height/2,_width*_endPos-30,0);
			}
			else{
				bounds=new Rectangle(_startPos*_width+30,_height/2,(_width-(_startPos*_width+30)),0);
			}
			
			_activeHandle.startDrag(false,bounds);
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE,_onMouseMove);
			
			_handleLeft.removeEventListener(MouseEvent.MOUSE_OVER,_onHandleMouseOver);
			_handleRight.removeEventListener(MouseEvent.MOUSE_OVER,_onHandleMouseOver);
			
			_handleLeft.removeEventListener(MouseEvent.MOUSE_OUT,_onHandleMouseOut);
			_handleRight.removeEventListener(MouseEvent.MOUSE_OUT,_onHandleMouseOut);
			stage.addEventListener(MouseEvent.MOUSE_UP,_onHandleMouseUp);
		}
		
		private function _onMouseMove(event:MouseEvent):void{
			_startPos=_handleLeft.x/_width;
			_endPos=_handleRight.x/_width;
			_drawHandles();
		}
		
		private function _onHandleMouseUp(event:MouseEvent):void{
			_activeHandle.stopDrag();
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,_onMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP,_onHandleMouseUp);
			
			_handleLeft.addEventListener(MouseEvent.MOUSE_OVER,_onHandleMouseOver);
			_handleRight.addEventListener(MouseEvent.MOUSE_OVER,_onHandleMouseOver);
			
			_handleLeft.addEventListener(MouseEvent.MOUSE_OUT,_onHandleMouseOut);
			_handleRight.addEventListener(MouseEvent.MOUSE_OUT,_onHandleMouseOut);
			
			_activeHandle.graphics.clear();
			_activeHandle.graphics.beginFill(0xFFFFFF,0.4);
			_activeHandle.graphics.drawCircle(0,0,8);
			_activeHandle.graphics.beginFill(0xFFFFFF,1);
			_activeHandle.graphics.drawCircle(0,0,5);
			_activeHandle.graphics.endFill();
			
			var evt:Event=new Event(LIMITS_CHANGED);
			dispatchEvent(evt);
		}
		
		public static const LIMITS_CHANGED:String="GraphNavigator.LimitsChanged";
				
		private function redraw():void{
			_drawHandles();
		}
	}
}